<template>
  <div class="major-wrapper">
    <div class="major-header-wrapper">
      <div class="major-header-search-wrapper">
        <el-input class="major-header-search-input"
                  prefix-icon="el-icon-search"
                  v-model="majorName"
                  placeholder="请输入专业名称进行搜索"></el-input>
        <el-button class="major-header-search-button"
                   icon="el-icon-search"
                   @click=""
                   type="primary">搜索</el-button>
      </div>
      <div class="major-header-operation-wrapper">
        <el-button class="major-header-add-button"
                   @click="showAddMajorDialog"
                   type="primary"
                   icon="el-icon-plus">添加专业</el-button>
        <el-button class="major-header-delete-batch-button"
                   type="danger"
                   @click="deleteMajorsByBatch"
                   :disabled="this.multipleSelections.length === 0"
                   icon="el-icon-delete">批量删除</el-button>
      </div>
    </div>
    <div class="major-table-wrapper">
      <el-table
              :data="majors"
              v-loading="loading"
              element-loading-text="拼命加载中"
              @selection-change="handleSelectionChange"
              stripe
              style="width: 100%">
        <el-table-column
                type="selection"
                width="55">
        </el-table-column>
        <el-table-column
                prop="id"
                label="编号"
                width="60">
        </el-table-column>
        <el-table-column
                prop="name"
                label="专业名称"
                width="200">
        </el-table-column>
        <el-table-column
                prop="department.name"
                label="所属学院"
                width="150">
        </el-table-column>
        <el-table-column
                prop="dean"
                label="系主任"
                width="150">
        </el-table-column>
        <el-table-column
                prop="phone"
                label="办公电话"
                width="130">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini"
                       @click="showEditMajorDialog(scope.row)"
                       type="primary">编辑</el-button>
            <el-button type="danger"
                       @click="deleteMajor(scope.row)"
                       size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog class="dialog"
               width="25%"
               :title="title"
               :visible.sync="dialogFormVisible">
      <div class="dialog-form-wrapper">
        <el-form class="dialog-form"
                 :rules="rules"
                 :model="major"
                 ref="majorForm">
          <el-row>
            <el-col :span="24">
              <el-form-item label="专业名称:" prop="name">
                <el-input v-model="major.name"
                          prefix-icon="el-icon-edit"
                          placeholder="请输入专业名称"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="所属学院:" prop="departmentId">
                <el-select v-model="major.departmentId"
                           placeholder="请选择学院">
                  <el-option
                          v-for="item in departments"
                          :key="item.id"
                          :label="item.name"
                          :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="系主任名字:" prop="dean">
                <el-input v-model="major.dean"
                          prefix-icon="el-icon-user"
                          placeholder="请输入系主任名字"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="办公电话:" prop="phone">
                <el-input v-model="major.phone"
                          prefix-icon="el-icon-phone-outline"
                          placeholder="请输入办公电话"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogMajor">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: "EduMajor",
    data() {
      return {
        majors: [],
        departments: '',
        majorName: '',
        multipleSelections: [],
        loading: false,
        title: '',
        dialogFormVisible: false,
        major: {
          id: null,
          name: '',
          departmentId: null,
          dean: '',
          phone: ''
        },
        rules: {
          name: [{ required: true, message: '请输入专业名称', trigger: 'blur' }],
          dean: [{ required: true, message: '请输入系主任名称', trigger: 'blur' }],
          departmentId: [{ required: true, message: '请选择所属学院', trigger: 'blur' }],
          phone: [{ required: true, message: '请输入办公电话', trigger: 'blur' }]
        }
      }
    },
    mounted() {
      this.getMajors()
      this.getDepartments()
    },
    methods: {
      //  获取所有专业
      getMajors() {
        this.loading = true
        this.getRequest('/edumanagement/major/').then(resp => {
          if (resp) {
            this.majors = resp
            this.loading = false
          }
        })
      },
      //  展示添加专业dialog
      showAddMajorDialog() {
        this.title = '添加专业'
        this.major = {
          id: null,
          name: '',
          departmentId: null,
          dean: '',
          phone: ''
        }
        this.dialogFormVisible = true
      },
      //  批量删除专业
      deleteMajorsByBatch() {
        this.$confirm('此操作将永久删除这['+ this.multipleSelections.length +']个专业, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let ids = '?'
          this.multipleSelections.forEach(item => {
            ids += 'ids=' + item.id + '&'
          })
          this.deleteRequest('/edumanagement/major/' + ids).then(resp => {
            if (resp) {
              this.getMajors()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      //  获取被选中的专业
      handleSelectionChange(data) {
        this.multipleSelections = data
      },
      //  展示更新专业dialog
      showEditMajorDialog(data) {
        this.title = '更新专业'
        //  this.updatePos = data 这个直接赋值会导致bug，输入框与table里的数据同步变化
        //  Object.assign(target, source1, source2)方法用于对象的合并，将源对象（source）的所有可枚举属性，复制到目标对象（target)
        Object.assign(this.major, data)
        this.dialogFormVisible = true
      },
      //  删除专业
      deleteMajor(data) {
        this.$confirm('此操作将永久删除['+ data.name +']专业, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.deleteRequest('/edumanagement/major/' + data.id).then(resp => {
            if (resp) {
              this.getMajors()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      //  添加或更新专业
      dialogMajor() {
        if (this.major.id) {
          //  更新专业
          this.$refs['majorForm'].validate((valid) => {
            if (valid) {
              this.dialogFormVisible = false
              this.putRequest('/edumanagement/major/', this.major).then(resp => {
                if (resp) {
                  this.getMajors()
                }
              })
            }
          })
        } else {
          //  添加专业
          this.$refs['majorForm'].validate((valid) => {
            if (valid) {
              this.dialogFormVisible = false
              this.postRequest('/edumanagement/major/', this.major).then(resp => {
                if (resp) {
                  this.getMajors()
                }
              })
            }
          })
        }
      },
      getDepartments() {
        this.getRequest('/edumanagement/major/department').then(resp => {
          if (resp) {
            this.departments = resp
          }
        })
      }
    }
  }
</script>

<style lang="less">

  .major-wrapper {
    width: 90%;
    margin-top: 20px;
    margin-left: 40px;

    .major-header-wrapper {
      display: flex;
      justify-content: space-between;
      margin-top: 20px;
      box-sizing: border-box;
      padding: 20px 20px;
      /*background-color: #EBEEF5;*/
      height: 80px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, .20), 0 0 6px rgba(0, 0, 0, .08);
      border-radius: 4px;

      .major-header-search-wrapper {
        display: flex;

        .major-header-search-input {
          margin-right: 10px;
        }

        .major-header-search-button {
          height: min-content;
        }

      }

      .major-header-operation-wrapper {

        .major-header-add-button {

        }

        .major-header-delete-batch-button {

        }
      }
    }

    .major-table-wrapper {
      margin-top: 20px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, .20), 0 0 6px rgba(0, 0, 0, .08);
      border-radius: 4px;
      padding: 5px 20px 20px 20px;
    }
  }
</style>